package com.ruoyi.erp.goodpath.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.erp.goodpath.domain.DeliveryRouteTab;
import com.ruoyi.erp.goodpath.service.GoodPathService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @version 2017.1.1
 * @Auther Du Ruifang @萌えキュンキュン
 * @Date 2020/12/21 09:43
 * @description 该类的作用
 */
@Api(tags = "交货路线")
@RestController
@RequestMapping("/erp/goodpath")
public class GoodPathController extends BaseController {
    @Resource
    private GoodPathService goodPathService;

    /**
     * 条件查询列表
     * @param deliveryRouteTab
     * @return
     */
    @PreAuthorize("@ss.hasPermi('erp:goodpath:list')")
    @GetMapping("/list")
    @ApiOperation(value="查询列表")
    public TableDataInfo list(DeliveryRouteTab deliveryRouteTab) {
        startPage();
        List<DeliveryRouteTab> deliveryRouteTabList = goodPathService.selectGoodPathList(deliveryRouteTab);
        return getDataTable(deliveryRouteTabList);
    }

    /**
     * 导出
     * @param deliveryRouteTab
     * @return
     */
    @PreAuthorize("@ss.hasPermi('erp:goodpath:export')")
    @GetMapping("/export")
    public AjaxResult export(DeliveryRouteTab deliveryRouteTab)
    {
        List<DeliveryRouteTab> deliveryRouteTabList = goodPathService.selectGoodPathList(deliveryRouteTab);
        ExcelUtil<DeliveryRouteTab> util = new ExcelUtil<DeliveryRouteTab>(DeliveryRouteTab.class);
        return util.exportExcel(deliveryRouteTabList, "交货路线数据");
    }
    @ApiOperation(value="校验工艺路线标识")
    @PostMapping("/checkRouteId")
    @ResponseBody
    public String checkRouteId (String routeId) {
        String b=goodPathService.checkRouteId(routeId);
        return b;
    }

    /**
     * 根据工艺路线标识获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('erp:goodpath:query')")
    @GetMapping(value = "/{routeId}")
    public AjaxResult getInfo(@PathVariable String routeId)
    {
        return AjaxResult.success(goodPathService.findGoodPath(routeId));
    }

    /**
     * 新增交货路线
     */
    @PreAuthorize("@ss.hasPermi('erp:goodpath:add')")
    @PostMapping
    public AjaxResult add(@Validated @RequestBody DeliveryRouteTab deliveryRouteTab) {
        try {
            goodPathService.saveGoodPath(deliveryRouteTab);
        } catch (Exception e) {
            e.printStackTrace();
            System.err.println(e.getMessage());
            return AjaxResult.error("添加失败");
        }
        return AjaxResult.success("添加成功");

    }

    /**
     * 修改保存交货路线
     */
    @PreAuthorize("@ss.hasPermi('erp:goodpath:edit')")
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody DeliveryRouteTab deliveryRouteTab) {
        try {
            goodPathService.editSaveGoodPath(deliveryRouteTab);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
            return AjaxResult.error("修改保存失败");
        }
        return AjaxResult.success("修改保存成功");
    }
    /**
     * 修改在航层检查
     */
    @PreAuthorize("@ss.hasPermi('erp:goodpath:edit')")
    @PutMapping("/changeCheckOnLineLevel")
    public AjaxResult changeCheckOnLineLevel(@RequestBody DeliveryRouteTab deliveryRouteTab) {
        try {
            goodPathService.editSaveGoodPath(deliveryRouteTab);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
            return AjaxResult.error("修改保存失败");
        }
        return AjaxResult.success("修改保存成功");
    }

    /**
     * 删除交货路线
     */
    @PreAuthorize("@ss.hasPermi('erp:goodpath:remove')")
    @DeleteMapping("/{routeIds}")
    public AjaxResult remove(@PathVariable String[] routeIds) {
        return toAjax(goodPathService.deleteGoodPathByIds(routeIds));
    }
}
